<?php
namespace jy_depot;
defined('BY_JYA') or exit('error');
/*
	仓库
*/
class depot extends \plugin{
	
	//删除仓库
	public function del_depot($depot_id){
		//删除门店
		$this->pdo_delete('depot',array('id'=>$depot_id));
		$this->pdo_delete('depot_goods',array('depot_id'=>$depot_id));
		//删除门店管理员
		$this->del_admin($depot_id);
		//删除门店操作员
		$clerks=$this->pdo_getall('clerk',array('uniacid'=>$_SESSION['uniacid'],'depot_id'=>$depot_id,'group_id >'=>0));
		if($clerks){
			foreach($clerks as $clerk){
				$this->del_oper($clerk['uid']);
			}
		}
		//删除仓库日志
		$this->pdo_delete('log',array('depot_id'=>$depot_id));
		return true;
	}
	//添加仓库管理员
	public function create_admin($params){
		$oper=array_fields(array('name','password'),$params);
		$oper['home']='jy_depot/index/';
		$uid=model('core/operator')->create_plugin_operator('jy_depot',$oper);
		$clerk=array(
			'uid'=>$uid,
			'uniacid'=>$_SESSION['uniacid'],
			'depot_id'=>$params['depot_id'],
			'group_id'=>0,
			'name'=>$params['name'],
			'createtime'=>TIMESTAMP,
		);
		$this->pdo_insert('clerk',$clerk);
		return true;
	}
	//修改仓库管理员
	public function update_admin($params){
		if(!$params['depot_id'] || !$params['name']){
			return;
		}
		$clerk=$this->pdo_get('clerk',array('depot_id'=>$params['depot_id'],'group_id'=>0));
		if(!$clerk){
			exi('更新门店管理员失败，找不到指定操作员！','error');
		}
		
		if($params['password'] || $params['name']!=$clerk['name']){
			$oper=array_fields(array('name','password'),$params);
			model('core/operator')->update_operator($clerk['uid'],$oper);
		}
		if($params['name']!=$clerk['name']){
			$this->pdo_update('clerk',array('name'=>$params['name']),array('uid'=>$clerk['uid']));
		}
		
		return true;
	}
	//删除仓库管理员
	public function del_admin($depot_id){
		$uid=$this->pdo_getcolumn('clerk',array('depot_id'=>$depot_id,'group_id'=>0),'uid');
		if(!$uid){
			return;
		}
		model('core/operator')->del_operator($uid);
		$this->pdo_delete('clerk',array('uid'=>$uid));
		return true;
	}
	//出库
	public function in_goods($data){
		$id=$this->pdo_getcolumn('depot_goods',array('depot_id'=>$data['depot_id'],'goods_id'=>$data['goods_id']),'id');
		if($id){
			$this->pdo_update('depot_goods',array('total +='=>$data['num']),array('id'=>$id));
		}else{
			$new_goods=array(
				'uniacid'=>$_SESSION['uniacid'],
				'depot_id'=>$data['depot_id'],
				'goods_id'=>$data['goods_id'],
				'goodstype'=>intval($data['goodstype']),
				'total'=>$data['num'],
			);
			$this->pdo_insert('depot_goods',$new_goods);
		}
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'uid'=>$_SESSION['uid'],
			'type'=>1,
			'createtime'=>TIMESTAMP,
		);
		$log=array_merge($data,$insert);
		$this->pdo_insert('log',$log);
		if(!$data['goodstype']){
			pdo_update('jy_weishop_goods',array('total +='=>$data['num']),array('id'=>$data['goods_id']));
		}
		return true;
	}
	//出库
	public function out_goods($data,$depot_goods){
		$this->pdo_update('depot_goods',array('total -='=>$data['num']),array('id'=>$id));
		if(!$depot_goods['goodstype']){
			pdo_update('jy_weishop_goods',array('total -='=>$data['num']),array('id'=>$depot_goods['goods_id']));
		}
		$data['goods_id']=$depot_goods['goods_id'];
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'uid'=>$_SESSION['uid'],
			'type'=>2,
			'createtime'=>TIMESTAMP,
		);
		$log=array_merge($data,$insert);
		$this->pdo_insert('log',$log);
		return true;
	}
}
